package model;

import java.util.List;

import tools.ChessPieceFactory;

public class Jeu {

	private Couleur couleur;
	private List<Pieces> listePieces;

	Boolean deplacementDiag;

	public Jeu(Couleur couleur) {
		this.couleur = couleur;
		this.listePieces = ChessPieceFactory.newPieces(couleur);
	}

	public boolean capture(int xCatch, int yCatch){
		Pieces toCapture = this.getPieceByPosition(xCatch, yCatch);
		return toCapture.capture();
	}

	public Couleur	getCouleur(){
		return couleur;
	}

	private Pieces getPieceByPosition(int x, int y)
	{
		for(Pieces p : this.listePieces)
		{
			if(p.getX() == x && p.getY() == y)
			{
				return p;
			}
		}

		return null;
	}

	public Couleur	getPieceCouleur(int x, int y) {
		return (null != getPieceByPosition(x,y) ? getPieceByPosition(x, y).getCouleur() : null);
	}

	public java.lang.String	getPieceName(int x, int y){
		return (null != getPieceByPosition(x,y) ? getPieceByPosition(x, y).getName() : null);
	}

	public java.lang.String	getPieceType(int x, int y){
		Pieces p = getPieceByPosition(x,y);
		String type = p.getClass().getName();
		if(type != null)
		{
			return type;
		}
		return null;
	}

	public boolean	isMoveOk(int xInit, int yInit, int xFinal, int yFinal){
		Pieces pieceMove = this.getPieceByPosition(xInit, yInit);
		return (pieceMove != null ? pieceMove.isMoveOk(xFinal, yFinal) : false);
	}

	public boolean	isPieceHere(int x, int y){
		return (null != getPieceByPosition(x,y) ? true : false );
	}

	public boolean	isPieceToCatchHere(int x, int y){
		return isPieceHere(x,y);
	}

	public boolean	isPieceToMoveHere(int x, int y){
		return isPieceHere(x, y);
	}

	public boolean	Move(int xInit, int yInit, int xFinal, int yFinal){
		Pieces toMove = this.getPieceByPosition(xInit, yInit);		
		return (toMove != null ? toMove.move(xFinal, yFinal) : false);
	}

	public java.lang.String	toString(){
		String temp = null;
		temp = "Couleur : "+this.couleur;
		for(Pieces p : listePieces){
			temp = temp + "\t" + p.getName() + "\n";
		}
		return temp;
	}
}
